package com.bandit.orm.test;

import test.db.DBManager;

/**
 * 关联关系测试数据初始化 多对多关系：用户和角色 一对多关系：字典类型和字典项
 *
 * @author gislin
 */
public class RelationTestDataInit {

    private String userTable = "TEST_BC_USER";
    private String roleTable = "TEST_BC_ROLE";
    private String orgTable = "TEST_BC_ORG";
    private String siteTable = "TEST_BC_SITE";
    private String roleUserTable = "TEST_BC_ROLE_USER";
    private String dictTypeTable = "TEST_BC_DICT_TYPE";
    private String dictDataTable = "TEST_BC_DICT_DATA";

    /**
     * 初始化数据表
     */
    public void initManyToManyTable() {

        String dropUserTable = "DROP TABLE " + userTable;
        String dropRoleTable = "DROP TABLE " + roleTable;
        String dropOrgTable = "DROP TABLE " + orgTable;
        String dropRoleUserTable = "DROP TABLE " + roleUserTable;
        String dropSiteTable = "DROP TABLE " + siteTable;

        String createUserTable =
            "create table " + userTable + "\n" + "(\n" + "  ID            VARCHAR2(50) not null,\n"
                + "  ORG_ID        VARCHAR2(50),\n" + "  REAL_NAME     VARCHAR2(50)\n" + ")";

        String addUserIndex = "alter table " + userTable + "\n" + "  add constraint " + userTable
            + "_ID primary key (ID)";

        String createSiteTable =
            "create table " + siteTable + "\n" + "(\n" + "  ID            VARCHAR2(50) not null,\n"
                + "  MN        VARCHAR2(50),\n" + "  SITE_NAME     VARCHAR2(50)\n" + ")";

        String addSiteIndex = "alter table " + siteTable + "\n" + "  add constraint " + siteTable
            + "_ID primary key (ID)";

        String createRoleTable =
            "create table " + roleTable + "\n" + "(\n" + "  ID         VARCHAR2(50),\n"
                + "  ORG_ID     VARCHAR2(50),\n" + "  NAME       VARCHAR2(100)\n" + ")";

        String addRoleIndex = "alter table " + roleTable + "\n" + "  add constraint " + roleTable
            + "_ID primary key (ID)";

        String createOrgTable =
            "create table " + orgTable + "\n" + "(\n" + "  ID         VARCHAR2(50),\n"
                + "  NAME       VARCHAR2(100)\n" + ")";

        String addOrgIndex = "alter table " + orgTable + "\n" + "  add constraint " + orgTable
            + "_ID primary key (ID)";

        String createRoleUserTable =
            "create table " + roleUserTable + "\n" + "(\n" + "  ID         VARCHAR2(50),\n"
                + "  ROLE_ID    VARCHAR2(50),\n" + "  USER_ID    VARCHAR2(50)\n" + ")";

        String addRoleUserIndex =
            "alter table " + roleUserTable + "\n" + "  add constraint " + roleUserTable
                + "_ID primary key (ID)";

        try {
            DBManager.getInstance().getDataExecuter().ddl(dropUserTable);
        }
        catch (Exception e) {
            // TODO: handle exception
        }

        try {
            DBManager.getInstance().getDataExecuter().ddl(dropRoleTable);
        }
        catch (Exception e) {
            // TODO: handle exception
        }

        try {
            DBManager.getInstance().getDataExecuter().ddl(dropRoleUserTable);
        }
        catch (Exception e) {
            // TODO: handle exception
        }

        try {
            DBManager.getInstance().getDataExecuter().ddl(dropOrgTable);
        }
        catch (Exception e) {
            // TODO: handle exception
        }

        try {
            DBManager.getInstance().getDataExecuter().ddl(dropSiteTable);
        }
        catch (Exception e) {
            // TODO: handle exception
        }

        DBManager.getInstance().getDataExecuter().ddl(createRoleUserTable);
        DBManager.getInstance().getDataExecuter().ddl(addRoleUserIndex);
        DBManager.getInstance().getDataExecuter().ddl(createUserTable);
        DBManager.getInstance().getDataExecuter().ddl(addUserIndex);
        DBManager.getInstance().getDataExecuter().ddl(createRoleTable);
        DBManager.getInstance().getDataExecuter().ddl(addRoleIndex);
        DBManager.getInstance().getDataExecuter().ddl(createOrgTable);
        DBManager.getInstance().getDataExecuter().ddl(addOrgIndex);
        DBManager.getInstance().getDataExecuter().ddl(createSiteTable);
        DBManager.getInstance().getDataExecuter().ddl(addSiteIndex);
    }

    public void initOneToManyTable() {
        String dropDictTypeTable = "DROP TABLE " + dictTypeTable;
        String dropDictDataTable = "DROP TABLE " + dictDataTable;

        String createDictTypeTable = "create table " + dictTypeTable + "\n" + "(\n"
            + "  ID            VARCHAR2(50) not null,\n" + "  CODE        VARCHAR2(50),\n"
            + "  NAME     VARCHAR2(50)\n" + ")";

        String addDictTypeIndex =
            "alter table " + dictTypeTable + "\n" + "  add constraint " + dictTypeTable
                + "_ID primary key (ID)";

        String addDictTypeUnique =
            "alter table " + dictTypeTable + "\n" + "  add constraint " + dictTypeTable
                + " unique (CODE)";

        String createDictDataTable =
            "create table " + dictDataTable + "\n" + "(\n" + "  ID         VARCHAR2(50),\n"
                + "  DICT_TYPE_CODE     VARCHAR2(50),\n" + "  CODE     VARCHAR2(50),\n"
                + "  VALUE       VARCHAR2(100)\n" + ")";

        String addDictDataIndex =
            "alter table " + dictDataTable + "\n" + "  add constraint " + dictDataTable
                + "_ID primary key (DICT_TYPE_CODE, CODE)";

        try {
            DBManager.getInstance().getDataExecuter().ddl(dropDictTypeTable);
        }
        catch (Exception e) {
            // TODO: handle exception
        }

        try {
            DBManager.getInstance().getDataExecuter().ddl(dropDictDataTable);
        }
        catch (Exception e) {
            // TODO: handle exception
        }

        DBManager.getInstance().getDataExecuter().ddl(createDictTypeTable);
        DBManager.getInstance().getDataExecuter().ddl(addDictTypeIndex);
        DBManager.getInstance().getDataExecuter().ddl(createDictDataTable);
        DBManager.getInstance().getDataExecuter().ddl(addDictDataIndex);
        DBManager.getInstance().getDataExecuter().ddl(addDictTypeUnique);
    }
}
